package edu.scut.ga.reconf;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.interpss.display.AclfOutFunc;
import org.junit.Test;

import com.interpss.common.datatype.UnitType;
import com.interpss.core.CoreObjectFactory;
import com.interpss.core.aclf.AclfNetwork;
import com.interpss.core.algo.LoadflowAlgorithm;
import com.interpss.core.net.Branch;

public class ReconfMinLossGATest {

	@Test
	public void testReconfMinLossGA() {
		// 1. 生成网络
		ReconfMinLossGA rmlga = new ReconfMinLossGA("testData/ieee70cdf.xls");
		AclfNetwork ieeeNetwork = rmlga.getNetwork();
		// 2. 计算潮流
	  	LoadflowAlgorithm algo = CoreObjectFactory.createLoadflowAlgorithm(ieeeNetwork);
	  	algo.loadflow();
	  	System.out.println(AclfOutFunc.loadFlowSummary(ieeeNetwork));
	  	System.out.println(ieeeNetwork.net2String());
	  	// 3. 计算网损
	  	float loss = 0;
	  	for (Branch thisBranch : ieeeNetwork.getBranchList()) {
	  		double thisCurrent = ieeeNetwork.getAclfBranch(thisBranch.getId()).current(UnitType.Amp);
	  		double thisVB = ieeeNetwork.getAclfBus(ieeeNetwork.getAclfBranch(thisBranch.getId()).getFromBusId()).getBaseVoltage() / 1000.0;
	  		double thisSB = ieeeNetwork.getBaseKva() / 1000.0;
	  		double thisR = ieeeNetwork.getAclfBranch(thisBranch.getId()).getZ().getReal() * thisVB * thisVB / thisSB;
	  		loss += thisCurrent * thisCurrent * thisR;
	  	}
	  	loss /= 1000.0;
	  	System.out.println("Total loss = " + loss + "kW");
		assertTrue(loss > 0);
	}
	
	@Test
	public void testNanhaiReconfMinLossGA() throws IOException {
		// 1. 生成网络
		ReconfMinLossGA rmlga = new ReconfMinLossGA("testData/桂城0711.xls");
		AclfNetwork ieeeNetwork = rmlga.getNetwork();
//	  	System.out.println(ieeeNetwork.net2String());
	  	FileWriter myFile = new FileWriter(new File("d://network.txt"));
	  	myFile.write(ieeeNetwork.net2String());
	  	myFile.flush();
	  	FileWriter myObject = new FileWriter(new File("d://network_serialized.txt"));
	  	myObject.write(ieeeNetwork.serialize());
	  	myObject.flush();
		// 2. 计算潮流
		if (!rmlga.getData().isConnected(ieeeNetwork))
			System.out.println("The network is not connected.");
	  	LoadflowAlgorithm algo = CoreObjectFactory.createLoadflowAlgorithm(ieeeNetwork);
	  	boolean isConverged = algo.loadflow();
	  	if (isConverged)
	  		System.out.println(AclfOutFunc.loadFlowSummary(ieeeNetwork));
	  	else
	  		System.out.println("Load flow cannot converge!");
	  	// 3. 计算网损
	  	float loss = 0;
	  	for (Branch thisBranch : ieeeNetwork.getBranchList()) {
	  		double thisCurrent = ieeeNetwork.getAclfBranch(thisBranch.getId()).current(UnitType.Amp);
	  		double thisVB = ieeeNetwork.getAclfBus(ieeeNetwork.getAclfBranch(thisBranch.getId()).getFromBusId()).getBaseVoltage() / 1000.0;
	  		double thisSB = ieeeNetwork.getBaseKva() / 1000.0;
	  		double thisR = ieeeNetwork.getAclfBranch(thisBranch.getId()).getZ().getReal() * thisVB * thisVB / thisSB;
	  		loss += thisCurrent * thisCurrent * thisR;
	  	}
	  	loss /= 1000.0;
	  	System.out.println("Total loss = " + loss + "kW");
		assertTrue(loss > 0);
		rmlga.implementGA();
	}

	@Test
	public void testImplementGA() {
		// 1. 生成网络
		ReconfMinLossGA rmlga = new ReconfMinLossGA("testData/ieee70cdf.xls");
		rmlga.implementGA();
	}
	
	@Test
	public void testImplementGAInNanhaiFull() {
		// 1. 生成网络
		ReconfMinLossGA rmlga = new ReconfMinLossGA("testData/桂城（完整版）.xls");
		rmlga.implementGA();
	}
	
	@Test
	public void testImplementGAInNanhaiSimplified() {
		// 1. 生成网络
		ReconfMinLossGA rmlga = new ReconfMinLossGA("testData/桂城（简化版）.xls");
		rmlga.implementGA();
	}
	
}